<template>
  <div class="article">
    <el-row >
      <el-col :span="16">
    <div class="wrapper">
      <div class="top">
        <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
          <el-form-item label="餐饮名称" prop="name">
            <el-input
                v-model="queryParams.name"
                placeholder="请输入菜品名称"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click="handleQuery"
            >搜索
            </el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="art">
        <el-row :gutter="20">
          <div
              class="left"
              v-for="item in cookingList"
              :key="item.id"
          >
            <el-col :span="6"
            >
              <div class="grid-content bg-purple">
                <div class="pic"><img width="100%" height="200px" :src="item.pic"/></div>
                <div class="text">
                  <div class="name">菜名：{{ item.name }}</div>
                  <div class="name">价格：{{ item.price }}元</div>
                  <div class="name">剩余：{{ item.stockNum }}份</div>
                  <div class="name"><el-input-number @change="addShopping(item)" v-model="item.num" size="mini" :min="0" :max="item.stockNum" label="描述文字"></el-input-number></div>
<!--                  <div class="name">-->
<!--                    <el-button type="success" @click="addShopping(item)">加入购物车</el-button>-->
<!--                  </div>-->
                </div>
                <div class="top"></div>
              </div
              >
            </el-col>
          </div>
        </el-row>
        <div class="page">
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page.currentPage"
              :page-sizes="[8, 12, 16]"
              :page-size="page.size"
              layout="total, sizes, prev, pager, next"
              :total="page.total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
      </el-col>
      <el-col :span="8">
        <h3>购物车</h3>
        <el-card class="box-card">
            <el-row>
              <el-col :span="8">餐饮名</el-col>
              <el-col :span="8">单价</el-col>
              <el-col :span="8">数量</el-col>
            </el-row>

            <div v-for="item in carList" :key="item.id" >
              <el-row style="border-bottom: 1px grey solid;margin-top: 5px;margin-bottom: 5px">
              <el-col :span="8">{{ item.name }}</el-col>
              <el-col :span="8">{{ item.price }}</el-col>
              <el-col :span="8">{{ item.num }}</el-col>
              </el-row>
            </div>

          <div style="margin-top: 60px;text-align: center">
            <el-button type="success" round @click="submit()">提交预定</el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {addRestaurantOrder, listRestaurant} from "@/utils/request";
import {Message} from "element-ui";

export default {
  name: "Restaurant",
  data() {
    return {
      cookingList: [],
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        status:1,
        name: null,
        city: null,
      },
      carList:[],
      num: 0,
      // 表单参数
      form: {},
      // 分页
      page: {
        size: 8,
        currentPage: 1,
        total: 0,
        status: 1
      },
    };
  },
  methods: {
    findPageQuery() {
      listRestaurant(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.cookingList = res.rows;
          this.page.total = res.total;
        } else {
          this.$message({
            type: "error",
            message: this.message,
          });
        }
      });
    },
    handleSizeChange(val) {
      this.page.size = val;
      this.queryParams.pageNum = val;
      this.findPageQuery();
    },
    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.queryParams.pageNum = val;
      this.findPageQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.findPageQuery();
    },
    submit(){
      addRestaurantOrder(this.carList).then(res=>{
        if (res.code == 200) {
          Message.success("预定成功");
          localStorage.removeItem("food");
          this.carList = JSON.parse(localStorage.getItem("food"));
          this.$forceUpdate()
        }
      });
    },
    addShopping(row) {
      let arr = [];
      let food = localStorage.getItem("food");
      if (!food){
        arr.push(row);
        localStorage.setItem("food", JSON.stringify(arr));
      }else{
        arr = JSON.parse(food);
        if (arr.filter(a=>a.id == row.id)){
          let sz = arr.filter(a=>a.id !== row.id);
          if (row.num!==0){
            sz.push(row);
          }
          localStorage.setItem("food", JSON.stringify(sz));
        }else {
          arr.push(row);
          localStorage.setItem("food", JSON.stringify(arr));
        }
      }
      this.carList = JSON.parse(localStorage.getItem("food"));
      this.$forceUpdate()
    }
  },
  created() {
    this.carList = JSON.parse(localStorage.getItem("food"));
    this.findPageQuery();
  },
};
</script>

<style scoped lang='scss'>
.article{
  min-height:590px
}
.top {
  height: 40px;
  justify-content: center;
  align-items: center;
  display: flex;
}

.name {
  text-align: center;
}

.page {
  margin-left: 320px;
}
</style>
